<template>
  <div class="payment-actions">
    <div class="actions-container">
      <!-- 支付方式选择 -->
      <PaymentMethodSelector 
        v-model="selectedPaymentMethod"
        @change="handlePaymentMethodChange"
      />
      
      <!-- 缴费按钮区域 -->
      <div class="payment-button-area">
        <div class="payment-summary">
          <span class="summary-text">
            已选择 {{ selectedCount }} 项缴费项目，总计：
          </span>
          <span class="total-amount">¥{{ totalAmount }}</span>
        </div>
        
        <el-button 
          type="primary" 
          size="large"
          :disabled="!canPay"
          :loading="loading"
          @click="handlePayment"
          class="pay-button"
        >
          <el-icon><Money /></el-icon>
          立即缴费
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { Money } from '@element-plus/icons-vue'
import PaymentMethodSelector from './PaymentMethodSelector.vue'

// Props
const props = defineProps({
  selectedItems: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['payment', 'payment-method-change'])

// 选中的支付方式
const selectedPaymentMethod = ref('wechat')

// 计算属性
const selectedCount = computed(() => props.selectedItems.length)

const totalAmount = computed(() => {
  return props.selectedItems.reduce((total, item) => {
    return total + (item.feeNumber || 0)
  }, 0)
})

const canPay = computed(() => {
  return props.selectedItems.length > 0 && !props.loading
})

// 处理支付方式变化
const handlePaymentMethodChange = (method) => {
  emit('payment-method-change', method)
}

// 处理缴费
const handlePayment = () => {
  if (!canPay.value) return
  
  emit('payment', {
    items: props.selectedItems,
    paymentMethod: selectedPaymentMethod.value,
    totalAmount: totalAmount.value
  })
}
</script>

<style scoped>
.payment-actions {
  margin-top: 24px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.actions-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.payment-button-area {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e9ecef;
}

.payment-summary {
  display: flex;
  align-items: center;
  gap: 8px;
}

.summary-text {
  font-size: 14px;
  color: #606266;
}

.total-amount {
  font-size: 18px;
  font-weight: 600;
  color: #e74c3c;
}

.pay-button {
  min-width: 120px;
  height: 40px;
  font-size: 16px;
  font-weight: 600;
}

.pay-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .payment-button-area {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .payment-summary {
    justify-content: center;
  }
  
  .pay-button {
    width: 100%;
  }
}
</style>